package cn.stylefeng.guns.modular.gridsystem.netty.tcp;

import cn.hutool.core.util.HexUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ssy
 * @date 2022/4/12 10:26
 */
@Slf4j
public class MessageDecoder extends SimpleChannelInboundHandler<ByteBuf> {
//    @Override
//    protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
//
//        Message message = new Message();
//        message.setTimestamp(byteBuf.readLong());
//        message.setBasicInfo(HexUtil.encodeHexStr(ByteBufUtil.getBytes(byteBuf.readBytes(6))));
//        message.setId(HexUtil.encodeHexStr(ByteBufUtil.getBytes(byteBuf.readBytes(6))));
//
//        final ByteBuf firmwareBytes = byteBuf.readBytes(3);
//        message.setFirmware(String.format("%s.%s.%s", firmwareBytes.readByte(), firmwareBytes.readByte(), firmwareBytes.readByte()));
//        message.setHardware(byteBuf.readByte());
//        message.setProtocol(byteBuf.readShort());
//        message.setFlag(ByteBufUtil.hexDump(byteBuf.readBytes(3)));
//
//        Message.DataGroup dataGroup = new Message.DataGroup();
//        dataGroup.setStype(byteBuf.readByte());
//        dataGroup.setSampleRate(byteBuf.readShort() & 0xffff);
//        dataGroup.setBits(byteBuf.readByte());
//        dataGroup.setChannel(byteBuf.readByte());
//        dataGroup.setData(ByteBufUtil.getBytes(byteBuf.readBytes(dataGroup.getChannel() * 2048)));
//        message.setDataGroup(dataGroup);
//
//        message.setCrc(ByteBufUtil.hexDump(byteBuf.readBytes(2)));
//        message.setEnd(byteBuf.readByte());
//
//        out.add(message);
//
//    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {


        Message message = new Message();
        message.setTimestamp(byteBuf.readLong());
        //readBytes会新建一个Bytebuf对象，用完需要手动释放
        ByteBuf buf = byteBuf.readBytes(6);
        message.setBasicInfo(HexUtil.encodeHexStr(ByteBufUtil.getBytes(buf)));
        buf.release();

        ByteBuf buf1 = byteBuf.readBytes(6);
        message.setId(HexUtil.encodeHexStr(ByteBufUtil.getBytes(buf1)));
        buf1.release();


        final ByteBuf firmwareBytes = byteBuf.readBytes(3);
        message.setFirmware(String.format("%s.%s.%s", firmwareBytes.readByte(), firmwareBytes.readByte(), firmwareBytes.readByte()));
        message.setHardware(byteBuf.readByte());
        message.setProtocol(byteBuf.readShort());
        ByteBuf buf2 = byteBuf.readBytes(3);
        message.setFlag(ByteBufUtil.hexDump(buf2));
        buf2.release();

        Message.DataGroup dataGroup = new Message.DataGroup();
        dataGroup.setStype(byteBuf.readByte());
        dataGroup.setSampleRate(byteBuf.readShort() & 0xffff);
        dataGroup.setBits(byteBuf.readByte());
        dataGroup.setChannel(byteBuf.readByte());

        ByteBuf buf3 = byteBuf.readBytes(dataGroup.getChannel() * 2048);
        byte[] bytes = ByteBufUtil.getBytes(buf3);
        dataGroup.setData(bytes);
        buf3.release();
        message.setDataGroup(dataGroup);

        ByteBuf buf4 = byteBuf.readBytes(2);
        message.setCrc(ByteBufUtil.hexDump(buf4));
        buf4.release();
        message.setEnd(byteBuf.readByte());
        System.out.println("     ");
        log.info("mqtt的 message >>" + message.getDataGroup());
        System.out.println("     ");
        ctx.fireChannelRead(message);
    }
}
